/**
 * 
 */
package furniture.render3d.primitives;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import org.eclipse.swt.graphics.RGB;

import furniture.render3d.Line3d;
import furniture.render3d.Polygon3d;
import furniture.render3d.Triangle;
import furniture.render3d.TriangleSet;


/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class Cube implements TriangleSet {

	private final List<TriangleSet> ts = new ArrayList<TriangleSet>();
	
	public Cube(boolean fill, double size, RGB color, int split){
		Point3d p11 = new Point3d(0,0,0);
		Point3d p12 = new Point3d(size,0,0);
		Point3d p13 = new Point3d(size,0,size);
		Point3d p14 = new Point3d(0,0,size);
		Point3d p21 = new Point3d(0,size,0);
		Point3d p22 = new Point3d(size,size,0);
		Point3d p23 = new Point3d(size,size,size);
		Point3d p24 = new Point3d(0,size,size);
		
		if (fill){
		//bottom
		ts.add(new Triangle(p11,p12,p13).split(split));
		ts.add(new Triangle(p11,p13,p14).split(split));
		//top
		ts.add(new Triangle(p21,p22,p23).split(split));
		ts.add(new Triangle(p21,p23,p24).split(split));
		
		ts.add(new Triangle(p11,p21,p12).split(split));
		ts.add(new Triangle(p21,p22,p12).split(split));
		
		ts.add(new Triangle(p11,p14,p24).split(split));
		ts.add(new Triangle(p11,p21,p24).split(split));
		
		ts.add(new Triangle(p14,p24,p23).split(split));
		ts.add(new Triangle(p14,p13,p23).split(split));
		
		ts.add(new Triangle(p22,p12,p13).split(split));
		ts.add(new Triangle(p22,p13,p23).split(split));
		for (TriangleSet qs : ts){
			for(Polygon3d q : qs.getTriangles()){
				Triangle t = (Triangle)q;
				t.fill = fill;
				t.paint = color;
			}
		}
		}else{
			ts.add(new Line3d(p11,p12).split(split));
			ts.add(new Line3d(p12,p13).split(split));
			ts.add(new Line3d(p13,p14).split(split));
			ts.add(new Line3d(p14,p11).split(split));
			
			ts.add(new Line3d(p21,p22).split(split));
			ts.add(new Line3d(p22,p23).split(split));
			ts.add(new Line3d(p23,p24).split(split));
			ts.add(new Line3d(p24,p21).split(split));
			
			ts.add(new Line3d(p11,p21).split(split));
			ts.add(new Line3d(p12,p22).split(split));
			ts.add(new Line3d(p13,p23).split(split));
			ts.add(new Line3d(p14,p24).split(split));
		}
		
	}
	
	/**
	 * @see furniture.render3d.TriangleSet#getTriangles()
	 */
	public Collection<Polygon3d> getTriangles() {
		List<Polygon3d> t = new ArrayList<Polygon3d>();
		for(TriangleSet s : ts) t.addAll(s.getTriangles());
		return t;
	}

	public void translate(Vector3d vector) {
		for(TriangleSet t : ts) t.translate(vector);
	}

}
